package com.ymatou.iapi.topicrecbizer.testcase;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.topicrecbizer.parameter.NoteBean;
import com.ymatou.iapi.topicrecbizer.service.NoteCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;

public class Ts_Note {
	private static NoteBean noteBean;
	private static NoteCall noteCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("笔记推荐前台接口");
	}

	@Before
	public void caseUp() {
		noteBean = new NoteBean();
		noteCall = new NoteCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(noteCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Note_001() {
		Logger.start(true,"笔记推荐前台接口-检查接口返回公共信息 ");
		try {
			//设置参数
			String requestid="111111";
			String score = "tnscore";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			noteBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			noteBean.setSort("desc("+score+")");
			noteBean.setAppkey("1");
			noteBean.setRequestid(requestid);
			noteBean.setModule("homepage_topic_note");
			noteBean.setPlatform("appbuyer");
			noteBean.setLimit("0,10");
			
			noteCall.setData(noteBean);
			noteCall.callService();
			
			Logger.verifyEquals("OK",noteCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(noteCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, noteCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, noteCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyEquals(requestid, noteCall.getCommonInfo("requestid"), "验证requestid值是否为所传值");
			Logger.verifyNotNull(noteCall.getCommonInfo("hostip"), "验证是否有返回服务器ip");
			Logger.verifyEquals(score, noteCall.getCommonInfo("algorithm"), "验证返回排序方式是否与所传值相同");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Note_002() {
		Logger.start(true,"笔记推荐前台接口-检查接口每条数据的返回值 ");
		try {
			//设置参数
			String requestid="111111";
			String score = "tnscore";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			noteBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			noteBean.setSort("desc("+score+")");
			noteBean.setAppkey("1");
			noteBean.setRequestid(requestid);
			noteBean.setModule("homepage_topic_note");
			noteBean.setPlatform("appbuyer");
			noteBean.setLimit("0,10");
			
			noteCall.setData(noteBean);
			noteCall.callService();
			
			Logger.verifyEquals("OK", noteCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = noteCall.getHits();
			
			List<String> list = new ArrayList<String>();
			List<Integer> listInt = new ArrayList<Integer>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String id = json.getString("topicId");
				
				if (list.contains(id) == true) {

					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的id存在重复值" + id);

				}else{
					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的id不存在重复值" + id);
					list.add(id);
				}
				
				JSONArray noteIds = json.getJSONArray("noteIds");
				
				for (int j = 1; j <= noteIds.length(); j++) {
					String noteInfo = noteIds.get(j - 1).toString();
					JSONObject noteJsonObject = new JSONObject(noteInfo);
					int noteid =   (Integer) noteJsonObject.get("noteid");
					if (list.contains(noteid) == true) {

						Logger.verifyEquals(false, list.contains(noteid), "验证第"+ i + "个主题中返回的第"+j+"条笔记存在重复值" + noteid);
						
					} else {
						Logger.verifyEquals(false, list.contains(noteid), "验证第"+ i + "条返回数据的id不存在重复值" + noteid);
						listInt.add(noteid);
					}
					
				}
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Note_003() {
		Logger.start(true,"笔记推荐前台接口-检查接口每条数据的返回值是否齐全，并且有有效值");
		try {
			//设置参数
			String requestid="111111";
			String score = "tnscore";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			noteBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			noteBean.setSort("desc("+score+")");
			noteBean.setAppkey("1");
			noteBean.setRequestid(requestid);
			noteBean.setModule("homepage_topic_note");
			noteBean.setPlatform("appbuyer");
			noteBean.setLimit("0,10");
			
			noteCall.setData(noteBean);
			noteCall.callService();
			
			Logger.verifyEquals("OK", noteCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = noteCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 
			 //获取接口返回的key校验返回字段
			  Logger.verifyEquals(true,infoAry.has("topicWords") , "验证第"+i+"条记录返回结果中有topicWords这个key");
			  Logger.verifyEquals(true,infoAry.has("topicId") , "验证第"+i+"条记录返回结果中有topicId这个key");
			  Logger.verifyEquals(true,infoAry.has("noteIds") , "验证第"+i+"条记录返回结果中有noteIds这个key");
			  Logger.verifyEquals(true,infoAry.has("showTitle") , "验证第"+i+"条记录返回结果中有noteIds这个key");
			  
			  JSONArray noteIds = infoAry.getJSONArray("noteIds");
				
				for (int j = 1; j <= noteIds.length(); j++) {
					String noteInfo = noteIds.get(j - 1).toString();
					JSONObject noteJsonObject = new JSONObject(noteInfo);
					//String content =   (String) noteJsonObject.get("content");
					Logger.verifyEquals(true, noteJsonObject.has("content"), "验证第"+i+"个主题的第"+j+"条记录是否有返回content值");
					Logger.verifyEquals(true, noteJsonObject.has("noteid"), "验证第"+i+"个主题的第"+j+"条记录是否有返回noteid值");
					Logger.verifyEquals(true, noteJsonObject.has("pics"), "验证第"+i+"个主题的第"+j+"条记录是否有返回pics值");
				}
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
